﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace YandexWebmaster
{
    public partial class Браузер : Form
    {
        public Браузер()
        {
            InitializeComponent();
        }

        public Браузер(Form form1)
        {
            InitializeComponent(); 
        }

        private void webView21_NavigationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs e)
        {
            BD AccountBD = new BD();

            if (webView21.Source.Host == "super-service.su") {
                
                int beginTokenString = webView21.Source.ToString().IndexOf("access_token=")+13;
                int endTokenString = webView21.Source.ToString().IndexOf("&token_type");
                int beginExpiresString = webView21.Source.ToString().IndexOf("&expires_in=") +12;
                string oauth_token = webView21.Source.ToString().Substring(beginTokenString, endTokenString - beginTokenString);
                uint expires_in = Convert.ToUInt32(webView21.Source.ToString().Substring(beginExpiresString));

                Основная form1 = (Основная)this.Owner;
                form1.currentAccount.OauthToken = oauth_token;
                form1.currentAccount.Epires_in = expires_in;
                AccountBD.UpdateAccessToken(form1.currentAccount.ID, oauth_token, expires_in);
                this.Close();
            }
        }
    }
}
